<?php
	/**
	 * 配置类
	 * Enter description here ...
	 * @author cui
	 *
	 */
	class config extends base
	{
		private static $self;
		private $config;

		/**
		 * 构造函数
		 * Enter description here ...
		 */
		private function __construct()
		{
			$this->config = new stdClass();
		}

		/**
		 * 单例入口
		 * Enter description here ...
		 */
		public static function get_self()
		{
			if (self::$self)
			{
				return self::$self;
			}

			self::$self = new self();

			return self::$self;
		}

		/**
		 * 加载配置
		 * Enter description here ...
		 */
		public function open($path = 'config')
		{
			$file_name = $this->get_config_path($path);
			$this->include_config($file_name, $path);
		}

		/**
		 * 加载配置文件
		 * Enter description here ...
		 * @param unknown_type $file_name	文件路径
		 * @param unknown_type $path	配置数组中的key
		 */
		private function include_config($file_name, $path)
		{
			if ($file_name === FALSE)
			{
				return FALSE;
			}

			$this->config->$path = new array_to_object(include_once $file_name);
		}

		/**
		 * 获取文件路径
		 * Enter description here ...
		 * @param unknown_type $path
		 */
		private function get_config_path($path)
		{
			if (role::get_role() == 'framework')
			{
				$file_name = CONFIG_PATH;
			}
			else
			{
				$file_name = MODULE_PATH . role::get_name() . DS . 'config' . DS;
			}

			$file_name .= $path . '.php';

			if (file_exists($file_name))
			{
				return $file_name;
			}

			throw_exception::process(new Exception($file_name . '没有找到', 5));
		}

		/**
		 * 读取配置
		 * Enter description here ...
		 * @param unknown_type $key
		 */
		public function get_config()
		{
			return $this->config;
		}

		/**
		 * 读取配置，静态方法
		 * Enter description here ...
		 * @param unknown_type $key
		 */
		public static function get()
		{
			return self::get_self()->get_config();
		}
	}